import Mock from 'mockjs'
import qs from 'qs'

/** **==========================测试数据API(拦截ajax请求，代理响应数据)===============================****/
// 获取产品
Mock.mock(/(api\/products)/, function (options) {
  // const [body, type] = [options.body, options.type]
  console.debug('---mock---', options)
  const result = [
    {
      groupDesc: '网络',
      groupName: 'Network',
      apps: [
        {
          desc: '主机入侵防御系统',
          name: 'HIDS',
          url: 'http://hids.jdfmgt.com'
        },
        {
          desc: '网络监控',
          name: 'DNP',
          url: 'http://dnp.jdfmgt.com'
        },
        {
          desc: 'F5Mgt',
          name: 'F5Mgt',
          url: 'http://f5.jdfmgt.com'
        },
        {
          desc: 'WAF',
          name: 'WAF',
          url: 'http://waf.jdfmgt.com'
        },
        {
          desc: 'dting',
          name: 'dting',
          url: 'http://dt-monitor.cbpmgt.com'
        },
        {
          desc: 'UMC网络监控',
          name: 'umc_web3.0',
          url: 'http://umc.jdfmgt.com'
        }
      ]
    },
    {
      groupDesc: '弹性计算',
      groupName: 'Compute',
      apps: [
        {
          desc: '弹性搜索平台',
          name: 'jes',
          url: 'http://ops-jes.jdfmgt.com'
        },
        {
          desc: '分布式调度',
          name: 'schedule-console',
          url: 'http://schedule.jd.com'
        }
      ]
    },
    {
      groupDesc: '消息队列 MQ',
      groupName: 'MQ',
      apps: [
        {
          desc: '掘金',
          name: 'juejin',
          url: 'http://juejin.jdfcloud.com'
        },
        {
          desc: '融金',
          name: 'rongjin',
          url: 'http://rongjin.jdfcloud.com'
        },
        {
          desc: 'rongjin_balance',
          name: 'rongjin_balance',
          url: 'http://rongjin-f5.jdfcloud.com'
        },
        {
          desc: '告警中心',
          name: 'uwc',
          url: 'http://uwc-console.cbpmgt.com'
        }
      ]
    },
    {
      groupDesc: '管控',
      groupName: 'GK',
      apps: [
        {
          desc: '统一配置中心',
          name: 'concrete-console',
          url: 'http://concrete.jd.com'
        },
        {
          desc: '统一日志',
          name: 'DIGGER',
          url: 'http://digger.jd.com/'
        },
        {
          desc: '京东金融-JSF服务网关系统',
          name: 'jdjr-smp',
          url: 'http://smperp.jd.com'
        },
        {
          desc: 'RAMA平台',
          name: 'jpu',
          url: 'http://jpu.jdfmgt.com'
        },
        {
          desc: 'jrauth',
          name: 'jrauth',
          url: 'https://upms.jd.com/app/appcreate'
        }
      ]
    },
    {
      groupDesc: '数据库',
      groupName: 'Database',
      apps: [
        {
          desc: '数据标准',
          name: 'data_standards',
          url: 'http://ds.jdfmgt.com'
        },
        {
          desc: '京东数科开发数据库建模平台 ',
          name: 'dbcm',
          url: 'http://dbcm.jdfmgt.com/'
        },
        {
          desc: 'dbrep',
          name: 'dbrep',
          url: 'http://dbrep-mgt.cbpmgt.com/admin'
        },
        {
          desc: '分布式关系型数据库服务',
          name: 'drds',
          url: 'http://cds.jd.com'
        }
      ]
    },
    {
      groupDesc: '监控与管理',
      groupName: 'Monitor',
      apps: [
        {
          desc: '业务秒级监控',
          name: 'fine-monitor',
          url: 'http://fine.db.cbpmgt.com'
        },
        {
          desc: '天玑大数据管理平台',
          name: 'phecda',
          url: 'http://phecda.jdfcloud.com'
        },
        {
          desc: '服务监控与治理',
          name: 'SGM',
          url: 'http://sgm.jd.com'
        },
        {
          desc: '移动端监控',
          name: 'sgm-mobile',
          url: 'http://sgm-mobile.jd.com'
        },
        {
          desc: 'sgm前端监控',
          name: 'sgm-web',
          url: 'http://sgm-web.jd.com'
        },
        {
          desc: ' 性能测试平台',
          name: 'titans',
          url: 'http://localhost:8080/'
        }
      ]
    }
  ]

  return result
})

// 获取菜单列表
Mock.mock(/(api\/systemmenus)/, function (options) {
  // const [body, type] = [options.body, options.type]
  console.debug('---mock---', options)
  const result = [
    {
      name: 'user',
      icon: 'icon-deviceModel',
      children: [
        {
          name: 'userInfo'
        },
        {
          name: 'modify',
          children: [
            {
              name: 'pwdchange',
              permission: 'admin:B' // 手动指定资源
            },
            {
              name: 'usermodify',
              permission: true // 自动生成资源(user:modify:usermodify)
            }
          ]
        }
      ]
    },
    {
      name: 'icon',
      icon: 'logo-windows'
    },
    {
      name: 'list',
      icon: 'icon-toTables',
      children: [
        {
          name: 'tableList'
        },
        {
          name: 'search'
        }
      ]
    }
  ]
  return result
})

// 获取系统菜单资源权限
Mock.mock(/(api\/permissions)/, function (options) {
  // const [body, type] = [options.body, options.type]
  console.debug('---mock---', options)
  const result = {
    roles: ['system', 'admin'],
    // permissions: ['monitor:*', 'alarm:*', 'baseinfo:deviceability:*', 'baseinfo:*', 'baseinfo:port:*']
    permissions: ['user:*', 'admin:*']
  }
  return result
})

// 获取用户
Mock.mock(/(framework\/principal)/, function (options) {
  // const [body, type] = [options.body, options.type]
  console.debug('---mock---', options)
  const result = { username: 'jdjradmin' }
  return result
})

// 模拟扩展菜单
Mock.mock(/(test\/searchMenu)/, function (options) {
  // const [body, type] = [options.body, options.type]
  console.debug('---mock---', options)
  const result = [
    {
      name: 'dnp-console',
      path: '/test/test1'
    },
    {
      name: 'SGM',
      children: [
        {
          name: 'sgm-web-console',
          path: '/test/testHome?app=sgm-mobile-console'
        },
        {
          name: 'sgm-mobile-console',
          path: '/test/test1'
        }
      ]
    }
  ]
  return result
})

// -------------------------------------------------------------------------------------------------------//

// 应用是否存在
Mock.mock(/(app\/exists)/, function (options) {
  console.debug('---mock---', options)
  return ['sgm', 'dnp'].includes(options.body)
})

// 应用列表
Mock.mock(/(app\/list)/, function (options) {
  const url = options.url
  const params = qs.parse(url.substr(url.indexOf('?') + 1))
  const length = params.length
  const appName = params.appName
  const result = Mock.mock({
    [`data|${length}`]: [
      {
        'id|+1': 0,
        'appName|+1': appName || '@word(5,10)',
        'appType|1': ['java', 'tomcat'],
        ip: '@ip()',
        'noticeType|1': ['咚咚', '邮箱', '微信', '外呼', 'QQ'],
        'alarmLevel|1': ['调试', '通知', '警告', '严重', '紧急'],
        'instance|1-10000000': 100,
        'updateDate|+1': Date.now(),
        'date|+1': '@datetime()'
      }
    ],
    total: 100
  })
  console.debug('---mock---', options, '***** Request data:', params, '***** Response data:', result)
  return result
})
// 应用列表
Mock.mock(/(baseTable\/list)/, function (options) {
  const url = options.url
  const params = qs.parse(url.substr(url.indexOf('?') + 1))
  const length = params.length
  // const appName = params.appName
  const result = Mock.mock({
    [`data|${length}`]: [
      {
        'id|+1': 1,
        'appName|+1': `[@datetime() - @datetime()]、[@ip()]、[IDC @word(5,10)]`,
        'appType|1': ['APP', 'DNS', 'DB'],
        'appDesc|+1': `[@datetime()]、[@appType|1] 
[UMC]：IDC_Huawei_CRC错误增长告警清除；可工作时间处理；Time2019-04-17 05:26:00；IP172.24.152.50；Original：Apr 17 2019 05:25:59+08:00 BJM6-DSJ-ACC-H6855-738_739-37U-2 %%01DEVM/1/`,
        'appProcess|1': [30, 80, 100, 0],

        'date|+1': '@datetime()'
      }
    ],
    total: 100
  })
  return result
})

// 应用新增
Mock.mock(/(app\/add)/, function (options) {
  console.debug('---mock---', options, '***** Request data:', JSON.parse(options.body), '***** Response data:', 'success')
  return 'success'
})
// 应用开启
Mock.mock(/(app\/start)/, function (options) {
  console.debug('---mock---', options, '***** Request data:', JSON.parse(options.body), '***** Response data:', 'success')
  return 'success'
})
// 应用关闭
Mock.mock(/(app\/stop)/, function (options) {
  console.debug('---mock---', options, '***** Request data:', JSON.parse(options.body), '***** Response data:', 'success')
  return 'success'
})

// 应用删除
Mock.mock(/(app\/delete)/, function (options) {
  console.debug('---mock---', options, '***** Request data:', JSON.parse(options.body), '***** Response data:', 'success')
  return 'success'
})

// 应用修改
Mock.mock(/(app\/update)/, function (options) {
  console.debug('---mock---', options, '***** Request data:', JSON.parse(options.body), '***** Response data:', 'success')
  return 'success'
})

// 热插拔test模块测试
Mock.mock(/(test\/api)/, function (options) {
  console.debug('--test-mock---', options)
  return { sys: 'ok' }
})
// 告警事件表格数据
Mock.mock(/(framework\/eventList)/, function (options) {
  return {
    data: {
      total: 12,
      data: [
        {
          eventId: 'eventId' + parseInt(Math.random() * 100),
          eventType: 'ALARM',
          appName: 'DNP',
          ip: '172.25.5.117',
          levelFlag: 'NORMAL',
          messageLevel: 'EMERGENCY',
          status: 'RESERVED',
          startTime: '2018-02-05',
          ruleKey: 'ruleKey1',
          clearWay: 'ACTIVE'
        },
        {
          eventId: 'eventId' + parseInt(Math.random() * 100),
          eventType: 'ALARM',
          appName: 'DNP',
          ip: '172.25.5.117',
          levelFlag: 'ROOT',
          messageLevel: 'CRITICAL',
          status: 'ACTIVE',
          startTime: '2018-02-05',
          ruleKey: 'ruleKey1',
          clearWay: 'ACTIVE'
        },
        {
          eventId: 'eventId' + parseInt(Math.random() * 100),
          eventType: 'ALARM',
          appName: 'DNP',
          ip: '172.25.5.117',
          levelFlag: 'CHILD',
          messageLevel: 'WARNING',
          status: 'PROCESSING',
          startTime: '2018-02-05',
          ruleKey: 'ruleKey1',
          clearWay: 'ACTIVE'
        },
        {
          eventId: 'eventId' + parseInt(Math.random() * 100),
          eventType: 'ALARM',
          appName: 'DNP',
          ip: '172.25.5.117',
          levelFlag: 'CHILD',
          messageLevel: 'NOTIFICATION',
          status: 'COMPLETED',
          startTime: '2018-02-05',
          ruleKey: 'ruleKey1',
          clearWay: 'ACTIVE'
        }
      ]
    },
    msgLevelCountMap: {
      EMERGENCY: 1,
      CRITICAL: 1,
      WARNING: 1,
      NOTIFICATION: 1,
      DEBUGGING: 0
    }
  }
})

Mock.mock(/(detail\/chart)/, function (options) {
  // const params = qs.parse(options.url.substr(options.url.indexOf('?') + 1))
  const result = {
    xAxis: ['2019/02/15', '2019/02/16', '2019/02/17', '2019/02/18', '2019/02/19', '2019/02/20'],

    series: {
      SCHEDULE_TAASK_RUNNING: [400, 410, 440, 330, 430, 120],
      SCHEDULE_TASK_UNACCEPTED: [240, 140, 350, 320, 400, 250],
      SCHEDULE_TASK_ACCEPTED: [180, 300, 180, 150, 180, 420],
      SCHEDULE_TASK_FAILED: [130, 140, 100, 70, 90, 80],
      SCHEDULE_APP_SECTET: [30, 40, 40, 30, 50, 30],
      APP: [20, 25, 20, 30, 40, 20],
      SCHEDULE_TEST_APP: [10, 10, 10, 15, 15, 8],
      SCHEDULE_APP_SECTET1: [30, 40, 40, 30, 50, 30],
      SCHEDULE_APP_SECTET2: [30, 40, 40, 30, 50, 30],
      SCHEDULE_APP_SECTET3: [30, 40, 40, 30, 50, 30]
    }
    // config: {
    //   "SCHEDULE_TAASK_RUNNING": { "type": params.type  },
    //   "SCHEDULE_TASK_UNACCEPTED": { "type": params.type },
    //   "SCHEDULE_TASK_ACCEPTED": { "type": params.type },
    //   "SCHEDULE_TASK_FAILED": { "type": params.type },
    //   "SCHEDULE_APP_SECTET": { "type": params.type },
    //   "APP": { "type": params.type },
    //   "SCHEDULE_TEST_APP": { "type": params.type },
    //   "SCHEDULE_APP_SECTET1": { "type": params.type },
    //   "SCHEDULE_APP_SECTET2": { "type": params.type },
    //   "SCHEDULE_APP_SECTET3": { "type": params.type }
    // },
  }
  return result
})
Mock.mock(/(home\/alarmChart)/, function (options) {
  // const params = qs.parse(options.url.substr(options.url.indexOf('?') + 1))
  const result = {
    xAxis: ['2019/01', '2019/02', '2019/03', '2019/04', '2019/05', '2019/06', '2019/07', '2019/08', '2019/09', '2019/10', '2019/11', '2019/12'],

    series: {
      图例1: [400, 410, 130, 140, 100, 70, 90, 80, 130, 140, 100, 70],
      图例2: [240, 140, 30, 40, 40, 30, 50, 30, 30, 40, 40, 30]
    }
  }
  return result
})
Mock.mock(/(home\/memoryChart)/, function (options) {
  // const params = qs.parse(options.url.substr(options.url.indexOf('?') + 1))
  const result = {
    xAxis: ['2019/01', '2019/02', '2019/03', '2019/04', '2019/05', '2019/06', '2019/07', '2019/08', '2019/09', '2019/10'],

    series: {
      内存使用率: [30, 20, 46, 8, 12, 28, 50, 40, 10, 15]
    }
  }
  return result
})
Mock.mock(/(home\/searchChart)/, function (options) {
  // const params = qs.parse(options.url.substr(options.url.indexOf('?') + 1))
  const result = {
    xAxis: [],

    series: {
      内存使用率: []
    }
  }
  return result
})
Mock.mock(/(detail\/tableData)/, function (options) {
  return {
    total: 100,
    data: [
      {
        Id: parseInt(Math.random() * 100),
        group: 'ALARM',
        version: 'redis_3.2.3-1.5'
      },
      {
        Id: parseInt(Math.random() * 100),
        group: 'ALARM',
        version: 'redis_3.2.3-1.2'
      },
      {
        Id: parseInt(Math.random() * 100),
        group: 'ALARM',
        version: 'redis_3.2.3-1.3'
      },
      {
        Id: parseInt(Math.random() * 100),
        group: 'ALARM',
        version: 'redis_3.2.3-1.5'
      }
    ]
  }
})
Mock.mock(/(home\/chart)/, function (options) {
  return {
    xAxis: ['06/27', '06/26', '06/25', '06/25', '06/24', '06/23', '06/22', '06/21'],
    series: {
      TPS峰值: [1200, 2100, 1600, 3100, 1400, 120, 1700, 4200]
    }
  }
})
Mock.mock(/(home\/piechart)/, function (options) {
  return {
    series: [
      {
        图例2: 75,
        图例3: 25
      },
      {
        图例1: 50,
        图例3: 50
      }
    ]
  }
})
Mock.mock(/(home\/radarchart)/, function (options) {
  console.debug('---mock---', options)
  const series = {}
  const config = {
    indicator: {
      '2.1.3': { max: 100 },
      其他: { max: 100 },
      '1.5.2': { max: 100 },
      '2.1.1': { max: 100 },
      '2.1.2': { max: 100 }
    }
  }
  // let { legends, startTime, endTime } = qs.parse(options.url.substr(options.url.indexOf('?') + 1)) // JSON.parse(options.body);
  const legends = [''];
  (Array.isArray(legends) ? legends : [legends]).forEach((legend) => {
    series[legend] = [Mock.Random.natural(0, 100), Mock.Random.natural(0, 100), Mock.Random.natural(0, 100), Mock.Random.natural(0, 100), Mock.Random.natural(0, 100), Mock.Random.natural(0, 100), Mock.Random.natural(0, 100), Mock.Random.natural(0, 100)]
  })
  return { series: series, config: config }
})
Mock.mock(/(home\/appList)/, function (options) {
  const url = options.url
  const params = qs.parse(url.substr(url.indexOf('?') + 1))
  const length = params.length
  // const appName = params.appName
  const result = Mock.mock({
    [`data|${length}`]: [
      {
        'id|+1': 1,
        'appName|+1': ['sgm-console-front', 'credit-platform-offline', 'wxgrowing_goldenpig'],
        'status|1': [0, 1],
        'jsErrorTotal|1-10000000': 100,
        'pageTotal|1-10000000': 100,
        'pageCostAvg|1-10000000': 100,
        'apiTotal|1-10000000': 100,
        'apiCostAvg|1-10000000': 100,

        'created_date|+1': '@datetime()',
        'modified_date|+1': '@datetime()'
      }
    ],
    total: 100
  })
  return result
})
Mock.mock(/(monitor\/cardList)/, function (options) {
  const result = Mock.mock({
    [`data|10`]: [
      {
        'id|+1': 1,
        title: '应用名应用名应用名应用名',
        desc: '中文描述中文描',
        'color|1': ['#F05A28', '#5EB95E', '#FF9904', '#E8C300'],
        circleData: { text: 'CPU', 'percent|1-100': 100, 'color|1': ['#F05A28', '#5EB95E', '#FF9904', '#E8C300'] },
        'CPU|1-100': 100,
        'memory|1-100': 100,
        'disk|1-100': 100,
        'network|1-100': 100,
        'waring|1-100': 100,
        'created_date|+1': '@datetime()',
        'modified_date|+1': '@datetime()'
      }
    ],
    total: 10
  })
  return result
})
Mock.mock(/(analysis\/chart)/, function (options) {
  return {
    xAxis: ['11:59:00', '12:07:00', '12:15:00', '12:23:00', '12:31:00'],
    series: {
      TPS峰值: [800, 200, 100, 600, 100, 100, 100, 110, 115, 118, 120, 118, 115, 110, 700, 700, 700, 200]
    },
    config: {
      TPS峰值: { type: 'line', unit: '', color: '#1C6AC9' }
    }
  }
})
Mock.mock(/(analysis\/analysisChart)/, function (options) {
  // const params = qs.parse(options.url.substr(options.url.indexOf('?') + 1))
  const result = {
    xAxis: ['2019/01', '2019/02', '2019/03', '2019/04', '2019/05', '2019/06', '2019/07', '2019/08', '2019/09', '2019/10', '2019/11', '2019/12'],

    series: {
      图例1: [40, 10, 30, 40, 100, 70, 90, 80, 30, 40, 100, 70],
      图例2: [40, 10, 30, 40, 100, 70, 90, 80, 30, 40, 100, 70],
      图例3: [40, 40, 30, 40, 40, 30, 50, 30, 30, 40, 40, 30]
    },
    config: {
      图例1: { type: 'bar', unit: '%', color: '#1C6AC9' },
      图例2: { type: 'bar', unit: '%', color: '#75b668' },
      图例3: { type: 'bar', unit: '%', color: '#e3c342' }
    }
  }
  return result
})
Mock.mock(/(analysis\/analysisLineChart)/, function (options) {
  // const params = qs.parse(options.url.substr(options.url.indexOf('?') + 1))
  const result = {
    xAxis: ['2019/01', '2019/02', '2019/03', '2019/04', '2019/05', '2019/06', '2019/07', '2019/08', '2019/09', '2019/10', '2019/11', '2019/12'],

    series: {
      图例1: [40, 10, 30, 40, 100, 70, 90, 80, 30, 40, 100, 70],
      图例2: [60, 30, 20, 30, 90, 80, 40, 50, 60, 50, 100, 80],
      图例3: [40, 40, 30, 40, 40, 30, 50, 30, 30, 40, 40, 30],
      图例4: [50, 30, 20, 50, 80, 50, 30, 60, 40, 30, 30, 40]
    },
    config: {
      图例1: { type: 'line', unit: '%', color: '#1C6AC9' },
      图例2: { type: 'line', unit: '%', color: '#202020' },
      图例3: { type: 'line', unit: '%', color: '#75b668' },
      图例4: { type: 'line', unit: '%', color: '#e3c342' }
    }
  }
  return result
})
Mock.mock(/(analysis\/piechart)/, function (options) {
  return {
    series:
    {
      跳转率: 75,
      留存率: 15,
      活跃率: 10
    }

  }
})
